package org.gwtunclutter.client;

import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.CellPanel;

/**
 * Defines style elements particular to a CellPanel, in addition
 * to those that apply to widgets in general.
 *
 * @author Alex
 */
public class CellPanelStyle extends WidgetStyle {
  private HasHorizontalAlignment.HorizontalAlignmentConstant[] horizontalAlignment;
  private HasVerticalAlignment.VerticalAlignmentConstant[] verticalAlignment;

  /**
   * Sets the horizontal and vertical alignment for every cell in the panel.
   */
  public CellPanelStyle(HasHorizontalAlignment.HorizontalAlignmentConstant[] horizontalAlignment, HasVerticalAlignment.VerticalAlignmentConstant[] verticalAlignment) {
    this.horizontalAlignment = horizontalAlignment;
    this.verticalAlignment = verticalAlignment;
  }

  public CellPanelStyle(String width, String height, HasHorizontalAlignment.HorizontalAlignmentConstant[] horizontalAlignment, HasVerticalAlignment.VerticalAlignmentConstant[] verticalAlignment) {
    super(width, height);
    this.horizontalAlignment = horizontalAlignment;
    this.verticalAlignment = verticalAlignment;
  }

  public CellPanelStyle(String styleName, String width, String height, HasHorizontalAlignment.HorizontalAlignmentConstant[] horizontalAlignment, HasVerticalAlignment.VerticalAlignmentConstant[] verticalAlignment) {
    super(styleName, width, height);
    this.horizontalAlignment = horizontalAlignment;
    this.verticalAlignment = verticalAlignment;
  }

  /** Applies the style to the given cell panel */
  public Widget apply(Widget widget) {
    CellPanel targetPanel = (CellPanel) widget;

    // apply general widget style
    super.apply(widget);

    // apply horizontal alignment constants
    if (horizontalAlignment != null) {
      for (int i = 0; i < horizontalAlignment.length; i++) {
        if (horizontalAlignment[i] != null)
          targetPanel.setCellHorizontalAlignment(targetPanel.getWidget(i), horizontalAlignment[i]);
      }
    }
    // apply vertical alignment constants
    if (verticalAlignment != null) {
      for (int i = 0; i < verticalAlignment.length; i++) {
      if (verticalAlignment[i] != null)
        targetPanel.setCellVerticalAlignment(targetPanel.getWidget(i), verticalAlignment[i]);
      }
    }
    return targetPanel;
  }

}
